package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;


    @Override
    public PageBeanVo findVideo(Long userId, Integer pageNum, Integer pageSize) {
        //1. 先查询推荐表,条件是userId=登录用户id
        Query query = new Query(
                Criteria.where("userId").is(userId)
        ).with(Sort.by(Sort.Order.desc("date")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        List<RecommendVideo> recommendVideoList = mongoTemplate.find(query, RecommendVideo.class);

        //2. 遍历集合,获取每个videoId,查询详情
        List<Video> videoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(recommendVideoList)) {
            for (RecommendVideo recommendVideo : recommendVideoList) {
                ObjectId videoId = recommendVideo.getVideoId();

                Video video = mongoTemplate.findById(videoId, Video.class);
                videoList.add(video);
            }
        }

        //3. 统计
        long count = mongoTemplate.count(query, RecommendVideo.class);

        //4. 返回
        return new PageBeanVo(pageNum, pageSize, count, videoList);
    }

    @Override
    public void saveVideo(Video video) {
        //1. 设置vid
        video.setVid(idService.getNextId(ConstantUtil.VIDEO_ID));

        //2. 保存
        mongoTemplate.save(video);

        //3. 模拟  将自己上传的视频推给自己
        RecommendVideo recommendVideo = new RecommendVideo();
        recommendVideo.setDate(System.currentTimeMillis());
        recommendVideo.setVid(video.getVid());
        recommendVideo.setUserId(video.getUserId());
        recommendVideo.setVideoId(video.getId());
        recommendVideo.setScore(100d);
        mongoTemplate.save(recommendVideo);
    }

    @Override
    public PageBeanVo findUserVideoList(Long uid, Integer pageNum, Integer pageSize) {
        Query query = new Query(
                Criteria.where("userId").is(uid)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        List<Video> videoList = mongoTemplate.find(query, Video.class);

        long count = mongoTemplate.count(query, Video.class);

        return new PageBeanVo(pageNum,pageSize,count,videoList);
    }
}
